interface userInfo {
  _id: string;
  name: string;
}
const userLists: userInfo[] = [
  { _id: '1001', name: 'vue' },
  { _id: '1002', name: 'react' },
  { _id: '1003', name: 'ts' }
];
const userListsObj: { [key: string]: userInfo } = {
  1001: { _id: '1001', name: 'vue' },
  1002: { _id: '1002', name: 'react' },
  1003: { _id: '1003', name: 'ts' }
};

// 将数组转为对象形式
const arrToObj = <T extends { _id?: string }>(arr: Array<T>): { [key: string]: T } => {
  return arr.reduce((prev, current) => {
    if (current._id) {
      prev[current._id] = current;
    }
    return prev;
  }, {} as { [key: string]: T });
};

console.log(arrToObj(userLists));

const objToArray = <T>(obj: { [key: string]: T }) => {
  return Object.keys(obj).map(k => obj[k]);
};
console.log(objToArray(userListsObj));
